layui.config({
    base: '../../static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index',//主入口模块
    treetable: 'treetable-lay/treetable'
}).use(['form']);

layui.use(['table','treetable','layer'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var $ = layui.$;

    table.render({
        elem:"#list",
        url: '../../admin/list',
        //查询条件
        where:{username: ''},
        id:"list",
        title:"用户数据表",
        cols:[[
            {field: 'username', id:'username',title: '用户名', width: 80, fixed: 'left', align: 'center'}
            , {field: 'userAccount', title: '用户账号', width: 120, align: 'center'}
            , {field: 'birth', title: '生日', width: 110, align: 'center'}
            , {
                field: 'sex', title: '性别', width: 60, align: 'center', templet: function (item) {
                    if (item.sex === 'F') {
                        return '女';
                    }
                    if (item.sex === 'M') {
                        return '男';
                    }
                }
            }
            , {field: 'email', title: '邮件', width: 120, align: 'center'}
            , {field: 'mobile', title: '电话', width: 150, align: 'center'}
            , {field: 'address', title: '地址', width: 120, align: 'center'}
            , {
                field: 'status', title: '状态', width: 80, align: 'center', templet: function (item) {
                    if (item.status === 'Y') {
                        return '启用';
                    }
                    if (item.status === 'N') {
                        return '禁用';
                    }
                }
            }
            // , {field: 'deptId', title: '部门ID', width: 76,  align: 'center'}
            //, {field: 'roleId', id:'role_id',title: '角色ID', width: 76, align: 'center'}
            , {field: 'roleName', title: '用户角色', width: 100, align: 'center'}
            , {field: 'name', title: '用户部门', width: 100, align: 'center'}
            // , {field: 'lockTime', title: '锁定时间', width: 150, align: 'center'}
            , {field: 'createTime', title: '创建时间', width: 150,  sort: true, align: 'center'}
            , {field: 'updateTime', title: '修改时间', width: 150, align: 'center'}
            , {field: 'createBy', title: '创建人', width: 80, align: 'center'}
            , {field: 'updateBy', title: '修改人', width: 80,  align: 'center'}
            , {fixed: 'right', title: '操作', toolbar: '#bar', width: 150, align: 'center'}
        ]]
        ,page:true
    });

    //条件查询搜索按钮
    $("#selectBtn").click(function (ev) {
        console.info("reload");
        table.reload('list', {
            where: {
                username: $('#username').val(),
            }, //设定异步数据接口的额外参数
            page: {
                curr:1
            }
        });
        return false;
    })

    //监听行工具事件
    table.on('tool(list)', function (obj) {
        var rowdata = obj.data
        debugger
        var userId = rowdata.userId;
        debugger
        var roleId=$("#role_id").val();

        //删除角色绑定的该用户
        if (obj.event === 'del') {
            layer.confirm('真的解除该角色绑定的用户么？', function (index) {
                debugger
                $.ajax({
                    type: "get",  //数据提交方式(post/get)
                    elem: "#bar",
                    data: "userId=" + userId,
                    url: "../../role/removeAssignUser",  //提交到的url
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",//返回的数据类型格式
                    success: function (Result) {
                        debugger
                        console.log(Result,'success');
                        layer.alert(("解除绑定成功！"),function (){
                            window.location.reload();//刷新父页面
                        });
                    }, error: function (e) {
                        debugger
                        console.log(e, 'error');
                        layer.msg("解除异常，请联系管理员！", {icon: 5, time: 1000});
                    }
                });
                layer.close(index);
            });
        }


        //给用户绑定角色
         if (obj.event === 'assign') {
            layer.confirm('确定绑定该角色么？', function (index) {

                debugger
                $.ajax({
                    type: "get",  //数据提交方式(post/get)
                    elem: "#bar",
                    data: {"roleId":roleId,"userId":userId},
                    //data: "userId="+userId,
                    url: "../../role/assignAddUser",  //提交到的url
                    //data: JSON.stringify(rowdata),
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function (Result) {
                        debugger
                        console.log(Result,'success');
                        layer.alert(("绑定角色成功！"),function (){
                            window.location.reload();//刷新父页面
                        })

                    }, error: function (e) {
                        debugger
                        console.log(e, 'error');
                        layer.msg("绑定异常，请联系管理员！", {icon: 5, time: 1000});
                    }
                });
                layer.close(index);
            });

        }
    });
    $('.demoTable .layui-btn').on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    })
});